/**
 * 随机向上滑动屏幕
 * @param {number} duration 滑动时长（毫秒）
 */
function randomSwipeUp(duration = 500) {
    let width = device.width;
    let height = device.height;

    // 设置起点坐标（横向中心 ± 偏移）
    let x1 = random(width * 0.45, width * 0.55);
    let y1 = random(height * 0.75, height * 0.85);

    // 设置终点坐标（向上滑动）
    let x2 = x1 + random(-20, 20); // 终点x可以微调
    let y2 = random(height * 0.15, height * 0.25);

    log(`滑动坐标: (${x1}, ${y1}) -> (${x2}, ${y2})`);

    swipe(x1, y1, x2, y2, random(duration-duration*0.2, duration+duration*0.2));
}


// let pkg = currentPackage();
// log("当前前台应用包名是: " + pkg);
// ========== 配置区 ==========
const PASSWORD = "123456"; // ⚠️ 替换为你的数字解锁密码
// ============================

if(!device.isScreenOn()){
    device.wakeUp(); // 唤醒屏幕
    sleep(1000); // 等待1秒
    randomSwipeUp()
    sleep(1000); // 等待1秒
    pwd=PASSWORD
    for (let i = 0; i < pwd.length; i++) {
        let digit = pwd[i];
        let btn = text(digit).findOne(3000);
        if (btn) {
            btn.parent().click();
            sleep(300);
        }
    }
}
function xiaomi_launch(package){
    launch(package);
    for (let i = 0; i < 3; i++) {
        sleep(300);
        // 获取当前前台应用的包名
        let cp = currentPackage();
        if(cp == undefined){
            continue
        }
        // 获取当前活动的Activity
        let ca = currentActivity();
        if(ca == undefined){
            continue
        }
        console.log("当前Activity:", ca);
        if(cp==package){
            return;
        }
        if(cp=="com.miui.securitycenter" && ca == "com.miui.wakepath.ui.ConfirmStartActivity"){
            let btn = text("允许").findOne(1000);
            print("输入数字: " + btn);
            if (btn) {
                btn.click();
                sleep(300);
                return;
            }
        }
    }
}
xiaomi_launch("com.tencent.wework")